package com.sangfor.ssl.common;

import android.content.Context;
import android.provider.Settings;
import android.text.TextUtils;
import com.sangfor.bugreport.logger.Log;
import com.sangfor.ssl.common.IDeviceIdStorage;
import java.util.HashMap;
import java.util.Map;

/* loaded from: classes2.dex */
public class DeviceIdStorageManager implements IDeviceIdStorage.ContentChangedListener {
    private static final String TAG = "DeviceIdStorageManager";
    private final Map<IDeviceIdStorage.IdType, String> mCachedIds;
    private Context mContext;
    private IDeviceIdStorage mPrivateStorage;
    private IDeviceIdStorage mSharedStorage;

    /* synthetic */ DeviceIdStorageManager(AnonymousClass1 anonymousClass1) {
        this();
    }

    public static DeviceIdStorageManager getInstance() {
        return InstanceHolder.INSTANCE;
    }

    public synchronized void init(Context context, IDeviceIdStorage iDeviceIdStorage, IDeviceIdStorage iDeviceIdStorage2) {
        if (this.mContext != null) {
            return;
        }
        if (context == null || iDeviceIdStorage == null || iDeviceIdStorage2 == null) {
            throw new IllegalArgumentException("context, privateStorage and sharedStorage cannot be null");
        }
        this.mContext = context;
        this.mPrivateStorage = iDeviceIdStorage;
        this.mSharedStorage = iDeviceIdStorage2;
        sync();
        this.mPrivateStorage.registerContentChangedListener(this);
        this.mSharedStorage.registerContentChangedListener(this);
    }

    public synchronized boolean isInited() {
        return this.mContext != null;
    }

    @Override // com.sangfor.ssl.common.IDeviceIdStorage.ContentChangedListener
    public synchronized void onContentChanged(IDeviceIdStorage iDeviceIdStorage) {
        if (isInited()) {
            Log.info(TAG, "onContentChanged, re-sync");
            sync();
        }
    }

    public synchronized String getId(IDeviceIdStorage.IdType idType) {
        return this.mCachedIds.get(idType);
    }

    private void sync() {
        IDeviceIdStorage.IdType[] values;
        try {
            this.mPrivateStorage.lockAndLoad();
            this.mSharedStorage.lockAndLoad();
            for (IDeviceIdStorage.IdType idType : IDeviceIdStorage.IdType.values()) {
                this.mCachedIds.put(idType, syncOneKey(idType));
            }
        } finally {
            this.mSharedStorage.saveAndRelease();
            this.mPrivateStorage.saveAndRelease();
        }
    }

    private String syncOneKey(IDeviceIdStorage.IdType idType) {
        String id = this.mPrivateStorage.getId(idType);
        if (!TextUtils.isEmpty(id)) {
            this.mSharedStorage.putId(idType, id);
            return id;
        }
        String id2 = this.mSharedStorage.getId(idType);
        if (!TextUtils.isEmpty(id2)) {
            this.mPrivateStorage.putId(idType, id2);
            return id2;
        } else if (AnonymousClass1.$SwitchMap$com$sangfor$ssl$common$IDeviceIdStorage$IdType[idType.ordinal()] == 1) {
            String androidId = getAndroidId();
            if (TextUtils.isEmpty(androidId)) {
                return null;
            }
            this.mSharedStorage.putId(idType, androidId);
            this.mPrivateStorage.putId(idType, androidId);
            return androidId;
        } else {
            throw new IllegalArgumentException("Unhandled type: " + idType);
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* renamed from: com.sangfor.ssl.common.DeviceIdStorageManager$1  reason: invalid class name */
    /* loaded from: classes2.dex */
    public static /* synthetic */ class AnonymousClass1 {
        static final /* synthetic */ int[] $SwitchMap$com$sangfor$ssl$common$IDeviceIdStorage$IdType;

        static {
            int[] iArr = new int[IDeviceIdStorage.IdType.values().length];
            $SwitchMap$com$sangfor$ssl$common$IDeviceIdStorage$IdType = iArr;
            try {
                iArr[IDeviceIdStorage.IdType.ANDROID_ID.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
        }
    }

    private String getAndroidId() {
        return Settings.Secure.getString(this.mContext.getContentResolver(), "android_id");
    }

    private DeviceIdStorageManager() {
        this.mCachedIds = new HashMap();
    }

    /* loaded from: classes2.dex */
    private static class InstanceHolder {
        static final DeviceIdStorageManager INSTANCE = new DeviceIdStorageManager(null);

        private InstanceHolder() {
        }
    }
}
